package com.sojpt.boot.controller.admin;

import java.util.Arrays;
import java.util.Date;

import com.jfinal.aop.Before;
import com.jfinal.kit.Kv;
import com.jfinal.kit.Ret;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;
import com.jfinal.plugin.activerecord.SqlPara;
import com.sojpt.boot.ui.iview.IViewController;
import com.sojpt.kit.IdKit;
import com.sojpt.model.TybkDeal;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 *
 * @author Sohnny
 *
 */
@RestController
@RequestMapping("/admin/deal")
public class TybkDealAdminController extends IViewController {


    @RequestMapping("/list")
    public void list() {

        TybkDeal tybkDeal = getModel(TybkDeal.class, "tybkDeal");
        tybkDeal.put("deal_date_signing_start_time", getPara("deal_date_signing_start_time"));
        tybkDeal.put("deal_date_signing_end_time", getPara("deal_date_signing_end_time"));
        tybkDeal.put("deal_issuing_date_start_time", getPara("deal_Issuing_date_start_time"));
        tybkDeal.put("deal_issuing_date_end_time", getPara("deal_Issuing_date_end_time"));
        tybkDeal.put("deal_annual_review_start_time", getPara("deal_annual_review_start_time"));
        tybkDeal.put("deal_annual_review_end_time", getPara("deal_annual_review_end_time"));
        tybkDeal.put("deal_expire_start_time", getPara("deal_expire_start_time"));
        tybkDeal.put("deal_expire_end_time", getPara("deal_expire_end_time"));
        tybkDeal.put("create_time_start_time", getPara("create_time_start_time"));
        tybkDeal.put("create_time_end_time", getPara("create_time_end_time"));
        tybkDeal.put("modification_time_start_time", getPara("modification_time_start_time"));
        tybkDeal.put("modification_time_end_time", getPara("modification_time_end_time"));
        tybkDeal.put("deletion_time_start_time", getPara("deletion_time_start_time"));
        tybkDeal.put("deletion_time_end_time", getPara("deletion_time_end_time"));

        SqlPara sqlPara = Db.getSqlPara("admin_deal.select", getKv(tybkDeal));
        Page<Record> page = Db.paginate(pageNumber(), pageSize(), sqlPara);
        renderJson(page);
    }
    @RequestMapping("/save")
    public void save() {
        TybkDeal tybkDeal = getModel(TybkDeal.class, "tybkDeal");
        tybkDeal.setId(IdKit.generatorID());
        tybkDeal.setCreateTime(new Date());
        tybkDeal.setDeleted(0);
        if(tybkDeal.save()) {
            renderJson(Ret.ok());
        } else {
            renderJson(Ret.fail());
        }
    }
    @RequestMapping("/update")
    public void update() {
        TybkDeal tybkDeal = getModel(TybkDeal.class, "tybkDeal");
        tybkDeal.setModificationTime(new Date());
        if(tybkDeal.update()) {
            renderJson(Ret.ok());
        } else {
            renderJson(Ret.fail());
        }
    }
    @RequestMapping("/delete")
    public void delete() {
        TybkDeal tybkDeal = getModel(TybkDeal.class, "tybkDeal");
        tybkDeal.setDeletionTime(new Date());
        tybkDeal.setDeleted(1);
        if(tybkDeal.update()) {
            renderJson(Ret.ok());
        } else {
            renderJson(Ret.fail());
        }
    }
    @RequestMapping("/batchDelete")
    public void batchDelete() {
        int a = Db.update("UPDATE tybk_deal SET deleted=1,deletion_time=NOW() WHERE id in" + "(" + getPara("ids") + ")");
        if (a > 0) {
            renderJson(Ret.ok());
        } else {
            renderJson(Ret.fail());
        }
    }

}